/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.hualu.zks.GUI;

import com.hualu.zks.CommonUtils.Globalparameters;
import com.hualu.zks.CommonUtils.ZKSPreferences;
import com.hualu.zks.Model.Node;
import com.hualu.zks.Dao.NodeDao;
import com.hualu.zks.Dao.UserDao;
import static com.hualu.zks.GUI.JFrame_File.nodeDao;
import com.hualu.zks.httpclient.GetFileList;
import java.awt.Desktop;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class MainWindow extends javax.swing.JFrame {
    /**
     * Creates new form mainWindow
     */
    private JPanel imagePanel;
    private ImageIcon background;
    public static String strVLCPath="";
    ZKSPreferences lgp;
    private static NodeDao nodeDao;
    
    public MainWindow() {
        initComponents();
        //setIconImage(Toolkit.getDefaultToolkit().getImage("./pic/0.jpg") );
        //设置背景图片
        background = new ImageIcon("./pic/bg.png");// 背景图片
        JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面
        // 把标签的大小位置设置为图片刚好填充整个面板
        label.setBounds(0, 0, background.getIconWidth(),
                        background.getIconHeight());
        // 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
        imagePanel = (JPanel) this.getContentPane();
        imagePanel.setOpaque(false);
        // 内容窗格默认的布局管理器为BorderLayout
        this.getLayeredPane().setLayout(null);
        // 把背景图片添加到分层窗格的最底层作为背景
        this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
        
        lgp=new ZKSPreferences(this);
        strVLCPath=lgp.getVLCPathName();
        //设置按钮背景
        btnUser.setIcon(new ImageIcon("./pic/user_no.png"));
        btnUser.setPressedIcon(new ImageIcon("./pic/user_yes.png"));
        btnUser.setContentAreaFilled(false);
        btnUser.setBorderPainted(false);
        btnUser.setFocusPainted(false);
        
        btnNode.setIcon(new ImageIcon("./pic/node_no.png"));
        btnNode.setPressedIcon(new ImageIcon("./pic/node_yes.png"));
        btnNode.setContentAreaFilled(false);
        btnNode.setBorderPainted(false);
        btnNode.setFocusPainted(false);
        
        btnFile.setIcon(new ImageIcon("./pic/file_no.png"));
        btnFile.setPressedIcon(new ImageIcon("./pic/file_yes.png"));
        btnFile.setContentAreaFilled(false);
        btnFile.setBorderPainted(false);
        btnFile.setFocusPainted(false);
        
        btnLuBo.setIcon(new ImageIcon("./pic/lubo_no.png"));
        btnLuBo.setPressedIcon(new ImageIcon("./pic/lubo_yes.png"));
        btnLuBo.setContentAreaFilled(false);
        btnLuBo.setBorderPainted(false);
        btnLuBo.setFocusPainted(false);

//        nodeDao = new NodeDao();
        //http心跳包
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                GetFileList getdata = new GetFileList();
//                int j = 1;
//                //尝试获取一次 检测刻录机是否在休眠
//                while (true) {
////                    System.err.println("心跳次数："+j);
//                    j++;
//                    for (int i = 0; i < nodeDao.getAllNode().size(); i++) {
//                        try {
////                            System.err.println(nodeDao.getAllNode().get(i).getIPAdd());
//                            while (1 == getdata.checkSleep(nodeDao.getAllNode().get(i).getIPAdd())) {
//                                try {
//                                    Thread.sleep(3 * 1000);
//                                } catch (InterruptedException ex) {
//                                    Logger.getLogger(JFrame_File.class.getName()).log(Level.SEVERE, null, ex);
//                                }
//                            }
//                        } catch (IOException ex) {
//                            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
//                        }
//                    }
//                    try {
//                        Thread.sleep(5 * 60 * 1000);
//                    } catch (InterruptedException ex) {
//                        Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                }
//            }
//        }).start();
        //        String url = Thread.currentThread().getContextClassLoader().getResource("").getPath().replaceAll("%20", " ");
        ////        System.out.println("url:"+url);
        //        //url为程序运行目录LGProject/build/classes/
        //        //打包后会发生变化
        //        String path = url + "logo1.png";
        //        this.setIconImage(this.getToolkit().getImage(path));
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnNode = new javax.swing.JButton();
        btnFile = new javax.swing.JButton();
        btnUser = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        btnLuBo = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        exitMenuItem = new javax.swing.JMenuItem();
        VLCMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentsMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("华录蓝光工程录像系统");
        setIconImage(Toolkit.getDefaultToolkit().getImage("pic\\bfhl.jpg"));
        setResizable(false);

        btnNode.setRequestFocusEnabled(false);
        btnNode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNodeActionPerformed(evt);
            }
        });

        btnFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFileActionPerformed(evt);
            }
        });

        btnUser.setRequestFocusEnabled(false);
        btnUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUserActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("微软雅黑", 0, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("欢迎使用华录班班通控制系统");

        btnLuBo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuBoActionPerformed(evt);
            }
        });

        menuBar.setMaximumSize(new java.awt.Dimension(100, 32769));

        fileMenu.setMnemonic('f');
        fileMenu.setText("菜单");

        exitMenuItem.setMnemonic('x');
        exitMenuItem.setText("退出");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        VLCMenu.setText("设置");
        VLCMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VLCMenuActionPerformed(evt);
            }
        });

        jMenuItem1.setText("VLC路径配置");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        VLCMenu.add(jMenuItem1);

        menuBar.add(VLCMenu);

        helpMenu.setMnemonic('h');
        helpMenu.setText("帮助");

        contentsMenuItem.setMnemonic('c');
        contentsMenuItem.setText("帮助文档");
        contentsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                contentsMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(contentsMenuItem);

        aboutMenuItem.setMnemonic('a');
        aboutMenuItem.setText("关于");
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnLuBo, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(btnUser, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                        .addComponent(btnNode, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(72, 72, 72)
                .addComponent(btnFile, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(71, 71, 71))
            .addGroup(layout.createSequentialGroup()
                .addGap(191, 191, 191)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(64, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnUser, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNode, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnFile, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addComponent(btnLuBo, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents
    
    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed
    
    private void btnNodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNodeActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        new JFrame_Node(this).setVisible(true);
        
    }//GEN-LAST:event_btnNodeActionPerformed
    
    private void btnUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUserActionPerformed
        // TODO add your handling code here:
        String user_type = UserDao.getUserType();
        if("1".equals(user_type)){
            this.setVisible(false);
            new JFrame_UserManagerAdmin(this, rootPaneCheckingEnabled).setVisible(true);
        } else {//为1跳到管理员的管理界面
        }
        if("0".equals(user_type)){
            this.setVisible(false);
            new JFrame_UserManager(this, rootPaneCheckingEnabled).setVisible(true);
        }
        if("-1".equals(user_type)){
            
            JOptionPane.showMessageDialog(null, "用户权限验证错误!", "警告", JOptionPane.WARNING_MESSAGE);
            //            System.out.println("用户权限验证错误:"+user_type);
        }
        
        
    }//GEN-LAST:event_btnUserActionPerformed
    
    private void btnFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFileActionPerformed
        // TODO add your handling code here:
        NodeDao nodeDao =new NodeDao();
        if(nodeDao.getAllNode().size()==0)
        {
            JOptionPane.showMessageDialog(null, Globalparameters.strSetNodePlease,Globalparameters.alarm,JOptionPane.WARNING_MESSAGE);
            return;
        }
        this.setVisible(false);
        try {
            new JFrame_File(this).setVisible(true);
        } catch (IOException ex) {
            Logger.getLogger(JFrame_File.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnFileActionPerformed
    
    private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
        // TODO add your handling code here:
        new About(this, true).setVisible(true);
    }//GEN-LAST:event_aboutMenuItemActionPerformed
    
    private void contentsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_contentsMenuItemActionPerformed
        try {
            // TODO add your handling code here:
            JOptionPane.showMessageDialog(null, Globalparameters.strSetPdfPlease,Globalparameters.alarm,JOptionPane.WARNING_MESSAGE);
            Desktop.getDesktop().open(new File("华录蓝光工程录像系统软件使用说明.pdf"));
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_contentsMenuItemActionPerformed
    
    private void VLCMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VLCMenuActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_VLCMenuActionPerformed
    
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        JFileChooser dlg = new JFileChooser();
        dlg.setFileSelectionMode(JFileChooser.FILES_ONLY);// 选择文件
        dlg.setDialogTitle(Globalparameters.strChooseVLCPath);// 设置窗口标题
        int result = dlg.showOpenDialog(this);// 打开"打开文件"对话框
        if (result == JFileChooser.APPROVE_OPTION) {
                strVLCPath=dlg.getSelectedFile().getPath();
//                System.out.println(dlg.getSelectedFile().getPath()); 
                lgp.setVLCPathName(strVLCPath);
               java.io.File[] filelist = dlg.getSelectedFiles();
               for (java.io.File file : filelist) {
                       System.out.println(file.getAbsolutePath());
               }
        }else{
          // 没有选择，点了取消后要做些什么
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void btnLuBoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuBoActionPerformed
        // TODO add your handling code here:
//        this.setVisible(false);
//        new JFrame_SerialPort(this).setVisible(true);
    }//GEN-LAST:event_btnLuBoActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainWindow().setVisible(true);
                
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu VLCMenu;
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JButton btnFile;
    private javax.swing.JButton btnLuBo;
    private javax.swing.JButton btnNode;
    private javax.swing.JButton btnUser;
    private javax.swing.JMenuItem contentsMenuItem;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuBar menuBar;
    // End of variables declaration//GEN-END:variables
    
}
